package com.songdehuai.motherandchild.widget.fabmenu;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

public class LabelView extends CardView {

    @ColorInt
    private int rightBackgroundColor;
    @ColorInt
    private int textColor;
    private TextView content;

    public LabelView(Context context, @ColorInt int backgroundColor) {
        super(context);
        rightBackgroundColor = backgroundColor;
        setCardBackgroundColor(0);
    }

    public LabelView(Context context, AttributeSet attrs, @ColorInt int backgroundColor) {
        super(context, attrs);
        rightBackgroundColor = backgroundColor;
        setCardBackgroundColor(0);
    }

    public LabelView(Context context, AttributeSet attrs, int defStyleAttr,
                     @ColorInt int backgroundColor) {
        super(context, attrs, defStyleAttr);
        rightBackgroundColor = backgroundColor;
        setCardBackgroundColor(0);
    }

    @Override
    public void setOnClickListener(@Nullable View.OnClickListener l) {
        if (l != null) {
            TypedValue outValue = new TypedValue();
            getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                                                     outValue, true);
            setForeground(ContextCompat.getDrawable(getContext(), outValue.resourceId));
            setClickable(true);
            setFocusable(true);
        } else {
            setForeground(null);
            setClickable(false);
            setFocusable(false);
        }
        super.setOnClickListener(l);
    }

    @Override
    public void setForeground(Drawable foreground) {
        super.setForeground(foreground);
    }

    public TextView getContent() {
        return content;
    }

    public void setContent(TextView content) {
        this.content = content;
    }

    @ColorInt
    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(@ColorInt int color) {
        textColor = color;
        content.setTextColor(color);
    }

    public void setTextColorFromRes(@ColorRes int color) {
        setTextColor(ContextCompat.getColor(getContext(), color));
    }

    public void setBackgroundColor(@ColorInt int backgroundColor) {
        rightBackgroundColor = backgroundColor;
        setCardBackgroundColor(0);
    }

    @Override
    public void setCardBackgroundColor(@ColorInt int ignored) {
        super.setCardBackgroundColor(rightBackgroundColor);
    }
}